package elitech.vietnam.myfashion.wsclient;

import java.io.IOException;
import java.util.ArrayList;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

public class Helper {

//	private static final String NAMESPACE = "http://192.168.1.14:800/";
//	private static final String URL = "http://192.168.1.14:800/services.php";
	
//	private static final String NAMESPACE = "http://192.168.1.18/";
//	private static final String URL = "http://192.168.1.18/services.php";
	
	private static final String NAMESPACE = "http://eli6.cafe24.com/shopping_services/";
	private static final String URL = "http://eli6.cafe24.com/shopping_services/services.php";
	
	public String sendRequest(String method, ArrayList<Param> params) throws IOException, XmlPullParserException {
		if (method == null || method.equals(""))
			return "";
		
		String action = NAMESPACE + method;
		SoapObject request = new SoapObject(NAMESPACE, method);
		if (params != null && !params.isEmpty())
			for (Param param : params)
				request.addProperty(param.name, param.value);
		
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.setOutputSoapObject(request);
		
		HttpTransportSE se = new HttpTransportSE(URL);
		System.setProperty("http.keepAlive", "false");
		
		se.call(action, envelope);
		SoapObject response = (SoapObject) envelope.bodyIn;
		
		String result = "";
		if (response != null) {
			result = response.getProperty(0).toString();
			result = result.trim();
		} else {
			result = "";
		}
		
		return result;
	}
	
	static class Param {
		String name;
		Object value;
		
		Param(String name, Object value) {
			this.name = name;
			this.value = value;
		}
	}
}
